package com.video.commons.utils;

import com.video.entity.ClientGroup;
import com.video.entity.ClientMsg;
import com.video.mapper.ClientGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GroupUtil {

    @Autowired
    private ClientGroupMapper clientGroupMapper;

    /**
     * 发送群组消息
     */
    public String getGroupInfo(){
        //组装信息发送

        StringBuilder sb = new StringBuilder("{");
        //编号
        sb.append("\"id\":").append("\"terminalGroup\",");

        sb.append(getGroups());

        sb.append("}");

        return sb.toString();
    }

    private String getGroups(){
        //组装信息发送
        List<ClientGroup> clientGroups = clientGroupMapper.queryAllGroupJoinClient();

        StringBuilder sb = new StringBuilder();
        //分组
        sb.append("\"groups\":[");

        sb.append(getGroupJson(clientGroups, -1));

        sb.append("]");

        return sb.toString();
    }

    private String getGroupJson(List<ClientGroup> clientGroups, Integer pid){

        StringBuilder sb = new StringBuilder();
        for (ClientGroup clientGroup : clientGroups) {
            if(clientGroup.getPid() == pid){

                if(sb.length() != 0){
                    sb.append(",");
                }
                //封装一个分组
                sb.append("{");
                if(clientGroup.getId() == 1){
                    sb.append("\"gname\":").append("\"").append("defualt group").append("\",");
                } else {
                    sb.append("\"gname\":").append("\"").append(clientGroup.getGname()).append("\",");
                }

                sb.append("\"level\":").append("" + countNum(clientGroups, clientGroup.getId()) + ",");
                sb.append("\"clients\":").append("[");

                //获得分组下的所有终端
                List<ClientMsg> clientMsgs = clientGroup.getClientMsgs();
                for (int i = 0; i < clientMsgs.size(); i++) {
                    if(i != 0){
                        sb.append(",");
                    }
                    sb.append("\"").append(clientMsgs.get(i).getUserid()).append("\"");
                }

                sb.append("],");

                sb.append("\"groups\":[");
                sb.append(getGroupJson(clientGroups, clientGroup.getId()));
                sb.append("]");

                sb.append("}");
            }
        }

        return sb.toString();
    }

    /**
     * 计算子组的层级
     * @param clientGroups
     * @param id
     * @return
     */
    private int countNum(List<ClientGroup> clientGroups, Integer id){
        int number = 0;
        if(clientGroups != null && clientGroups.size() > 0){
            for (ClientGroup clientGroup : clientGroups) {
                if(clientGroup.getId() == id){
                    if(clientGroup.getPid() != -1){
                        number += countNum(clientGroups, clientGroup.getPid());
                    }
                    number++;
                    break;
                }
            }
        }

        return number;
    }
}
